package com.kstudio.videocapture.z;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.Log;

public class KResoures {
    /* normal button size */
    public static File directory = null;
    public static int width__1 = 48;
    public static int height_1 = 48;

    public static final String Path_Btn_Repository_Disable = "Btn_Repository_Disable.png";
    public static final String Path_Btn_Repository_Enable  = "Btn_Repository_Enable.png";
    public static final String Path_Btn_Repository_Press   = "Btn_Repository_Press.png";

    public static final int Id_Btn_Repository_Disable = 0;//R.raw.btn_repository_disable;
    public static final int Id_Btn_Repository_Enable  = 0;//R.raw.btn_repository_enable;
    public static final int Id_Btn_Repository_Press   = 0;//R.raw.btn_repository_press;

    private static Bitmap saveBitmaptoCache(Resources res, File file, int id, int width, int height) {
        Bitmap bm = null;

        if(!file.exists()) {
            InputStream in = null;
            Bitmap inBm = null;
            try {
                in = res.openRawResource(id);
//                inBm = BitmapFactory.decodeStream(in);
                Options options = new BitmapFactory.Options();
                options.inScaled = false;
                options.inDither = false;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;

                inBm = BitmapFactory.decodeStream(in, null, options);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

//            Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
            bm = Bitmap.createScaledBitmap(inBm, width, height, false);

            OutputStream out = null;

            try {
                file.createNewFile();
                out = new FileOutputStream(file);

                bm.compress(CompressFormat.PNG, 100, out);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return bm;
    }

    private static Bitmap getBitmapFromCache(Resources res, File file, int id, int width, int height) {
        Bitmap bm = null;

        if(file.exists()) {
            InputStream in = null;

            try {
                in = new FileInputStream(file);
                bm = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if(bm == null) {
            bm = saveBitmaptoCache(res, file, id, width, height);
        }
        Log.d("KKH",String.format("getBitmapFromCache : bm %d %d",bm.getWidth(),bm.getHeight()));
        return bm;
    }

    private static StateListDrawable getStateListDrwable(Resources res,
            String pathDisable, String pathEnable, String pathPress,
            int idDisable, int idEnable, int idPress,
            int width, int height) {
            StateListDrawable drawable = new StateListDrawable();
            File file = null;
            Bitmap bm = null;

            file = new File(directory, pathDisable);
            bm = getBitmapFromCache(res, file, idDisable, width, height);
            drawable.addState(new int[] { -android.R.attr.state_enabled }, new BitmapDrawable(res, bm));

            file = new File(directory, pathPress);
//            bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, idPress), width, height, false);
            bm = getBitmapFromCache(res, file, idPress, width, height);
            Log.d("KKH",String.format("idPress : bm %d %d",bm.getWidth(),bm.getHeight()));
            drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed }, new BitmapDrawable(res, bm));

            file = new File(directory, pathEnable);
//            bm = BitmapFactory.decodeResource(res, idEnable);//Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, idEnable), width, height, false);
            bm = getBitmapFromCache(res, file, idEnable, width, height);

            InputStream in = null;
            Bitmap inBm = null;
            try {
                in = res.openRawResource(idEnable);

                Options options = new BitmapFactory.Options();
                options.inScaled = false;
                options.inDither = false;
//                options.inSampleSize = 3;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                inBm = BitmapFactory.decodeStream(in, null, options);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
//            bm = inBm;
            bm = Bitmap.createScaledBitmap(inBm, width, height, false);;

            Log.d("KKH",String.format("idEnable : bm %d %d",bm.getWidth(),bm.getHeight()));
            drawable.addState(new int[] { android.R.attr.state_enabled }, new BitmapDrawable(res, bm));

            return drawable;
    }

    public static void saveBitmaptoCaches(Resources res) {
        saveBitmaptoCache(res, new File(directory, Path_Btn_Repository_Disable), Id_Btn_Repository_Disable, width__1, height_1);
        saveBitmaptoCache(res, new File(directory, Path_Btn_Repository_Enable), Id_Btn_Repository_Enable, width__1, height_1);
        saveBitmaptoCache(res, new File(directory, Path_Btn_Repository_Press), Id_Btn_Repository_Press, width__1, height_1);
    }

    public static StateListDrawable getDrawable_Repository(Resources res) {
        return getStateListDrwable(res,
            Path_Btn_Repository_Disable, Path_Btn_Repository_Enable, Path_Btn_Repository_Press,
            Id_Btn_Repository_Disable, Id_Btn_Repository_Enable, Id_Btn_Repository_Press,
            width__1, height_1);
    }
}
